package com.bob.demo.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bob.demo.system.dao.UserRepository;
import com.bob.demo.system.model.Menu;
import com.bob.demo.system.model.Tree;
import com.bob.demo.system.model.User;
import com.bob.demo.system.service.IUserService;
import com.bob.demo.utils.StringUtils;

@RequestMapping("admin/user")
@Controller
public class UserController extends BaseController {


	@Autowired
    private UserRepository userRepository;
	
	@Autowired
    private IUserService userService;
	
	
	@Value("${test.msg}")
	private String msg;
	
	@GetMapping({ "/index" })
	public String index(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		String pageNoStr = request.getParameter("pageNo");
		String pageSizeStr= request.getParameter("pageSize");
		int pageNo = 0;
		int pageSize = 10;
		if(!StringUtils.isNullOrEmpty(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(!StringUtils.isNullOrEmpty(pageSizeStr)) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		Sort sort = new Sort(Direction.DESC, "createTime");
		Pageable pageable = new PageRequest(pageNo, pageSize, sort);
		Page<User> page = userRepository.findAll(pageable);
		
		System.err.println("size:"+page.getContent().size());
		model.addAttribute("page", page);
		model.addAttribute("list", page.getContent());
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("username", getUsername());
		return "amazeui/admin/user-list";
	}
	
	@GetMapping({"/main"})
	public String main(HttpServletRequest request, HttpServletResponse response,Model model) {
		System.err.println("666");
		return "amazeui/admin/main";
	}

}
